//
//  UILabel+MixText.m
//  beibei_ios
//
//  Created by pro on 16/7/1.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "UILabel+MixText.h"

#import "ZKFaceAttachment.h"

static const char mixTextRegexKey;
static const char mixTextKey;
static const char mixTextHeightKey;
static const char mixTextSizeKey;
static const char mixAttrAryKey;


@implementation UILabel (MixText)

- (void)setMixRegex:(NSString *)mixRegex {
    
    objc_setAssociatedObject(self, &mixTextRegexKey, mixRegex, OBJC_ASSOCIATION_COPY_NONATOMIC);

}
- (NSString *)mixRegex {
    return objc_getAssociatedObject(self, &mixTextRegexKey);

}

- (void)setMixAttrAry:(NSArray *)mixAttrAry {
    objc_setAssociatedObject(self, &mixAttrAryKey, mixAttrAry, OBJC_ASSOCIATION_COPY_NONATOMIC);

}
- (NSArray *)mixAttrAry {
    return objc_getAssociatedObject(self, &mixAttrAryKey);
}

- (void)setMixText:(NSString *)mixText {

    objc_setAssociatedObject(self, &mixTextKey, mixText, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    [self handMixText];
    
}
- (NSString *)mixText {
    
    return objc_getAssociatedObject(self, &mixTextKey);
}

- (void)setMixTextHeight:(CGFloat)mixTextHeight {
    
    objc_setAssociatedObject(self, &mixTextHeightKey, @(mixTextHeight), OBJC_ASSOCIATION_RETAIN);
}
- (CGFloat)mixTextHeight {
    return [objc_getAssociatedObject(self, &mixTextHeightKey) floatValue];

}
- (void)setMixTextSize:(CGSize)mixTextSize {
    
    objc_setAssociatedObject(self, &mixTextSizeKey, NSStringFromCGSize(mixTextSize), OBJC_ASSOCIATION_RETAIN);
}
- (CGSize)mixTextSize {
    return CGSizeFromString(objc_getAssociatedObject(self, &mixTextSizeKey));
    
}

#pragma mark - 辅助方法
- (void)handMixText {
    
    NSString *text = self.mixText;
    
    NSString *regex;
    if (self.mixRegex) {
        regex = self.mixRegex;
    } else {
        regex = @"\\[:f\\d{3}\\}";
    }
    
    NSMutableArray *nameAry = [[NSMutableArray alloc] init];
    
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regex options:0 error:nil];
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    
    if (reg != nil) {
        NSArray<NSTextCheckingResult *> *matchs = [reg matchesInString:text options:0 range:NSMakeRange(0, text.length)];
        for (NSTextCheckingResult *reult in matchs) {
            NSRange resultRange = [reult rangeAtIndex:0];
            NSString *name = [text substringWithRange:resultRange];
            [nameAry addObject:name];
        }
        
        // 创建 附件对象，把附件添加到 AttributedString中
        NSMutableString *mutableStr = [NSMutableString stringWithString:text];
        for (NSString *str in nameAry) {
            ZKFaceAttachment *attachment = [[ZKFaceAttachment alloc] init];
            NSString *name = [str substringWithRange:NSMakeRange(2, 4)];
            attachment.image = [UIImage imageNamed:name];
            NSString *nameTag = [name substringWithRange:NSMakeRange(1, 3)];
            if (nameTag.integerValue >= 100) {
                attachment.attachmentSize = CGSizeMake(30, 40);
            } else {
                attachment.attachmentSize = CGSizeMake(20, 20);
            }
            NSAttributedString *imageAttributedString = [NSAttributedString attributedStringWithAttachment:attachment];
            
            NSRange range = [mutableStr rangeOfString:str];
            
            [mutableStr replaceCharactersInRange:range withString:@" "];
            [attrString replaceCharactersInRange:range withAttributedString:imageAttributedString];
        }
    }
    
    if (self.mixAttrAry) {
        for (NSDictionary *dic in self.mixAttrAry) {
            NSString *name = dic[@"name"];
            id value = dic[@"value"];
            NSString *range = dic[@"range"];
            [attrString addAttribute:name value:value range:NSRangeFromString(range)];
        }
    }
    
    NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];
    ps.lineBreakMode = NSLineBreakByCharWrapping;
    [attrString addAttribute:NSParagraphStyleAttributeName value:ps range:NSMakeRange(0, attrString.length)];

    
    self.attributedText = attrString;
    self.mixTextSize = [self sizeThatFits:CGSizeMake(self.bounds.size.width, 100000)];
    self.mixTextHeight = [self sizeThatFits:CGSizeMake(self.bounds.size.width, 100000)].height;
}

@end
